// src/stores/AttractionStore.js
import { defineStore } from 'pinia'
import { 
  getAttractionByRegion, 
  getAttractionByRating,
  getAttractionBySelectCount,
  searchAttraction,
  getAttractionInfo 
} from '../api/Attraction'

export const useAttractionStore = defineStore('attraction', {
  state: () => ({
    attractions: [],
    currentPage: 1,
    pageSize: 10,
    hasMore: true,
    loading: false,
    selectedAttraction: null,
    filterType: 'region',
    searchQuery: '',
    selectedRegion: null
  }),

  actions: {
    async loadMoreAttractions() {
      if (this.loading || !this.hasMore) return
      
      this.loading = true
      try {
        let response
        switch (this.filterType) {
          case 'region':
            response = await getAttractionByRegion(
              this.selectedRegion, 
              this.currentPage,
              this.pageSize
            )
            break
          case 'rating':
            response = await getAttractionByRating(
              this.currentPage,
              this.pageSize
            )
            break
          case 'popularity':
            response = await getAttractionBySelectCount(
              this.currentPage,
              this.pageSize
            )
            break
          case 'search':
            response = await searchAttraction(
              this.currentPage,
              this.pageSize,
              this.searchQuery
            )
            break
        }

        if (response?.data) {
          const newItems = response.data.items || []
          this.attractions.push(...newItems)
          this.hasMore = newItems.length === this.pageSize
          this.currentPage++
        }
      } finally {
        this.loading = false
      }
    },

    resetList() {
      this.attractions = []
      this.currentPage = 1
      this.hasMore = true
    },

    async getAttractionDetail(id) {
      const response = await getAttractionInfo(id)
      if(response?.data) {
        this.selectedAttraction = response.data
      }
      return this.selectedAttraction
    }
  }
})